#include "Key.h"

#define KEY1  (DL_GPIO_readPins(Key_PORT, Key_SW1_PIN) ? 1 : 0)
#define KEY2  (DL_GPIO_readPins(Key_PORT, Key_SW2_PIN) ? 1 : 0)
#define KEY3  (DL_GPIO_readPins(Key_PORT, Key_SW3_PIN) ? 1 : 0)
#define KEY4  (DL_GPIO_readPins(Key_PORT, Key_SW4_PIN) ? 1 : 0)

#define KeyPort KEY1 | (KEY2 << 1) | (KEY3 << 2) | (KEY4 << 3) | 0XF0     //1111 XXXX
 
volatile uint8_t Key_Flag;

//三行法 独立按键
//KeyPort 1111 XXXX   -->  ReadData: 0000 XXXX  --> Key_Flag  : 0000 XXXX
void vKey_Scan(void)
{
    static uint8_t LastData;
    uint8_t ReadData = (KeyPort) ^ 0xFF;    //异或 相异为1 相同为0
    Key_Flag = ReadData & (ReadData ^ LastData);
    LastData = ReadData;
}

void vKey_Func(void)//一次按下功能
{      
    vKey_Scan();    //按键扫描
    switch(Key_Flag)
    {
        case 0x01://KEY1 UP
            vKey1_UP_Service();
            break;
        case 0x02://KEY2 Left

            vKey2_Left_Service();
            break;
        case 0x04://KEY3 Back

            vKey3_Back_Service();
            break;
        case 0x08://KEY4 Right

            vKey4_Right_Service();
            break;
        default:
        
            break;
    }
}

void vKey1_UP_Service(void)
{
    
    Sprintf("a");

}

void vKey2_Left_Service(void)
{
    Sprintf("b");
}


void vKey3_Back_Service(void)
{
    Sprintf("c");

}

void vKey4_Right_Service(void)
{
    

}